package com.wss.module.main.ui.main;

import com.orhanobut.logger.Logger;
import com.wss.common.base.BaseActionBarAbility;
import com.wss.common.base.BaseApplication;
import com.wss.common.base.BaseFragment;
import com.wss.common.base.Fragment;
import com.wss.common.base.mvp.BasePresenter;
import com.wss.common.constants.ARouterConfig;
import com.wss.common.utils.TextUtils;
import com.wss.common.utils.ToastUtils;
import com.wss.module.main.ResourceTable;
import com.wss.module.main.ui.main.fragment.CaseFragment;
import com.wss.module.user.ui.main.fragment.UserFragment;
import com.wss.module.wan.ui.main.fragment.HomeFragment;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.multimodalinput.event.KeyEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainAbility extends BaseActionBarAbility implements Component.ClickedListener {

    private long mExitTime;

    private DirectionalLayout mTabMain;
    private Text mTabMainText;
    private Image mTabMainImg;

    private DirectionalLayout mTabCenter;
    private Text mTabCenterText;
    private Image mTabCenterImg;

    private DirectionalLayout mTabUser;
    private Text mTabUserText;
    private Image mTabUserImg;

    private String mTabCurrent = "";

    /**
     * 存放切换Fragment
     */
    private List<Fragment> mFragmentList = new ArrayList<>();
    private Map<String, Fragment> mFragmentMap = new HashMap();

    /**
     * 玩模块Fragment
     */
    private BaseFragment wanFragment;

    /**
     * 我的模块Fragment
     */
    private BaseFragment userFragment;

    private StackLayout mContentLayout;

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_main_ability_main;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        initView();
    }

    protected void initView() {

//        wanFragment = new HomeFragment(this);
//        userFragment = new UserFragment(this);

        findView();
        setListener();
        getTitleBar().showBackImg(false);
        setCenterText(getString(ResourceTable.String_main_tab_center));
        changeTab(CaseFragment.class.getName());
    }

    private void findView() {
        mContentLayout = (StackLayout) findComponentById(ResourceTable.Id_fl_context);

        mTabMain = (DirectionalLayout) findComponentById(ResourceTable.Id_rb_main);
        mTabMainImg = (Image) findComponentById(ResourceTable.Id_rb_main_img);
        mTabMainText = (Text) findComponentById(ResourceTable.Id_rb_main_text);

        mTabCenter = (DirectionalLayout) findComponentById(ResourceTable.Id_rb_center);
        mTabCenterImg = (Image) findComponentById(ResourceTable.Id_rb_center_img);
        mTabCenterText = (Text) findComponentById(ResourceTable.Id_rb_center_text);

        mTabUser = (DirectionalLayout) findComponentById(ResourceTable.Id_rb_user);
        mTabUserImg = (Image) findComponentById(ResourceTable.Id_rb_user_img);
        mTabUserText = (Text) findComponentById(ResourceTable.Id_rb_user_text);
    }

    private void setListener() {
        mTabMain.setClickedListener(this);
        mTabCenter.setClickedListener(this);
        mTabUser.setClickedListener(this);
    }

    /**
     * Fragment 发生改变
     * @param tag Fragment 类名
     */
    public void changeFragment(String tag) {
        hideFragment();
        Fragment fragment = mFragmentMap.get(tag);
        if (fragment != null) {
            fragment.getRootView().setVisibility(Component.VISIBLE);
            showFragment(fragment);
        } else {
            if (TextUtils.equals(tag, ARouterConfig.WAN_MAIN_FRAGMENT)) {
                if (wanFragment == null) {
                    wanFragment = new HomeFragment(this);
                }
                fragment = wanFragment;
            } else if (TextUtils.equals(tag, CaseFragment.class.getName())) {
                fragment = new CaseFragment(this);
            } else if (TextUtils.equals(tag, ARouterConfig.USER_MAIN_FRAGMENT)) {
                if (userFragment == null) {
                    userFragment = new UserFragment(this);
                }
                fragment = userFragment;
            } else {
                fragment = new CaseFragment(this);
            }
            if (fragment == null) {
                return;
            }
            mFragmentList.add(fragment);
            mFragmentMap.put(tag, fragment);
            showFragment(fragment);
        }
    }

    private void showFragment(Fragment fragment) {
        mContentLayout.removeAllComponents();
        mContentLayout.addComponent(fragment.getRootView());
    }

    private void hideFragment() {
        for (Fragment fragment : mFragmentList) {
            if (fragment.getRootView() != null) {
                fragment.getRootView().setVisibility(Component.HIDE);
            }
        }
        mContentLayout.removeAllComponents();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
        Logger.e("==== on key down : " + keyCode + " , time : " + (System.currentTimeMillis() - mExitTime));
        if (keyCode == KeyEvent.KEY_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                ToastUtils.show(context, getString(ResourceTable.String_main_exit_app));
                mExitTime = System.currentTimeMillis();
            } else {
                terminateAbility();
            }
            return true;
        }
        return super.onKeyDown(keyCode, keyEvent);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void onClick(Component component) {
        int id = component.getId();
        if (id == ResourceTable.Id_rb_main) {

            changeTab(ARouterConfig.WAN_MAIN_FRAGMENT);
        } else if (id == ResourceTable.Id_rb_center) {

            changeTab(CaseFragment.class.getName());
        } else if (id == ResourceTable.Id_rb_user) {

            changeTab(ARouterConfig.USER_MAIN_FRAGMENT);
        }
    }

    private void changeTab(String tag) {
        if (TextUtils.equals(tag, mTabCurrent)) {
            return;
        }
        mTabCurrent = tag;
        changeFragment(tag);

        changeTabItem(mTabCenterImg,
                ResourceTable.Media_main_tab_center_default,
                mTabCenterText,
                getColor(ResourceTable.Color_gray));
        changeTabItem(mTabMainImg,
                ResourceTable.Media_main_tab_home_default,
                mTabMainText,
                getColor(ResourceTable.Color_gray));
        changeTabItem(mTabUserImg,
                ResourceTable.Media_main_tab_user_default,
                mTabUserText,
                getColor(ResourceTable.Color_gray));

        if (TextUtils.equals(tag, ARouterConfig.WAN_MAIN_FRAGMENT)) {

            changeTabItem(mTabMainImg,
                    ResourceTable.Media_main_tab_home_select,
                    mTabMainText,
                    getColor(ResourceTable.Color_blue));
            setCenterText(getString(ResourceTable.String_main_tab_home));

        } else if (TextUtils.equals(tag, CaseFragment.class.getName())) {

            changeTabItem(mTabCenterImg,
                    ResourceTable.Media_main_tab_center_select,
                    mTabCenterText,
                    getColor(ResourceTable.Color_blue));
            setCenterText(getString(ResourceTable.String_main_tab_center));

        } else if (TextUtils.equals(tag, ARouterConfig.USER_MAIN_FRAGMENT)) {

            changeTabItem(mTabUserImg,
                    ResourceTable.Media_main_tab_user_select,
                    mTabUserText,
                    getColor(ResourceTable.Color_blue));
            setCenterText(getString(ResourceTable.String_main_tab_user));

        }

    }

    private void changeTabItem(Image image, int imgRes, Text text, int colorRes) {
//        image.setImageAndDecodeBounds(imgRes);
        image.setPixelMap(imgRes);
        text.setTextColor(new Color(colorRes));
    }
}
